<?php


namespace app\api\controller\v1;


use app\api\model\ExchangeModel;
use app\api\model\UserModel;
use app\common\controller\UserBaseController;
use ApiOper;
use app\common\library\DLock;

class ExchangeController extends UserBaseController
{
    //商品列表
    function exchange_listAction()
    {
        $this->paramlist=[
            'token' => 'string',
            'city_id' => 'string',
            'pno' => 'int',
            'psize' => 'int',
            'city' => 'optstring',
        ];

        $this->check_params();
        $this->check_token();

        $city = $this->city_id;

        $cd=[];
        if(isset($this->argv['city'])){
            $city = $this->argv['city'];
        }
        else{
            $cd['user'] = 1;    //用户
        }

        $list = (new ExchangeModel())->card_list($city,$this->argv['pos'],$this->argv['limit'],$cd);

        $this->resp['list'] = $list;
        $this->resp['total'] = count($list);

        return $this->ret_resp();
    }

    function exchangeAction()
    {
        $this->paramlist=[
            'token' => 'string',
            'id' => 'string',
        ];

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['id'],5);

        $userinfo = (new UserModel())->query_info(ApiOper::instance()->userinfo()['id']);

        (new ExchangeModel())->submit($userinfo['city_id'],$userinfo,$this->argv['id']);

        return $this->ret_resp();
    }

    //兑换记录
    function recordlistAction()
    {
        $this->paramlist=[
            'token' => 'string',
            'pno' => 'int',
            'psize' => 'int',
        ];

        $this->check_params();
        $this->check_token();

        $cd =[
            'order_type' => SCORE_GATEWAY_EXCHANGE,
        ];

        $list = (new ExchangeModel())->record_list(ApiOper::instance()->userinfo()['id'],$this->argv['pos'],$this->argv['limit'],$cd);

        $this->resp['list'] = $list;
        $this->resp['total'] = count($list);

        return $this->ret_resp();
    }

    //添加商品
    function add_itemAction(){
        $this->paramlist=[
            'token' => 'string',
            'city' => 'optstring',              //城市限制  ,不传表示所有城市
            'item_type' => 'int',               //类型 type 1:会员卡 2:骑行卡 3:优惠卷 4实物
            'item_name' => 'string',            //名字
            'amount' => 'int',                  //积分
            'img' => 'string',                  //图片url
            'duration' => 'int',                //时长 秒为单位
            'discount' => 'int',                //每次兑换多少张，只有骑行卡有几次，其他默认一张
            'dismoney' => ['optint',0],         //优惠金额，优惠卷
            'free_count' => 'int',              //库存数量 0表示无限兑换
            'begin_time' => 'string',           //开始时间
            'end_time'   => 'string',           //结束时间
        ];


        $this->check_params();
        $this->check_token();


        $item=[
            'type' => $this->argv['item_type'],
            'name' => $this->argv['item_name'],
            'amount' => $this->argv['amount'],
            'img' => $this->argv['img'],
            'duration' => $this->argv['duration'],
            'discount' => $this->argv['discount'],
            'dismoney' => $this->argv['dismoney'],
            'free_count' => $this->argv['free_count'],
            'count' => $this->argv['free_count'],
            'begin_time' => $this->argv['begin_time'],
            'end_time' => $this->argv['end_time'],
        ];

        if(isset($this->argv['city'])){
            $item['city_id'] = $this->argv['city'];
        }

        (new ExchangeModel())->add_item($item);

        return $this->ret_resp();
    }

    //更新商品
    function update_itemAction(){
        $this->paramlist=[
            'token' => 'string',
            'id' => 'string',                   //商品id
            'city' => 'optstring',             //城市限制  ,不传表示所有城市
            'item_type' => 'int',               //类型 type 1:会员卡 2:骑行卡 3:优惠卷 4实物
            'item_name' => 'string',            //名字
            'amount' => 'int',                  //积分
            'img' => 'string',                  //图片url
            'duration' => 'int',                //时长 秒为单位
            'discount' => 'int',                //每次兑换多少张，只有骑行卡有几次，其他默认一张
            'dismoney' => ['optint',0],         //优惠金额，优惠卷
            'free_count' => 'int',              //库存数量 0表示无限兑换
            'begin_time' => 'string',           //开始时间
            'end_time'   => 'string',           //结束时间
        ];


        $this->check_params();
        $this->check_token();


        $item=[
            'type' => $this->argv['item_type'],
            'name' => $this->argv['item_name'],
            'amount' => $this->argv['amount'],
            'img' => $this->argv['img'],
            'duration' => $this->argv['duration'],
            'discount' => $this->argv['discount'],
            'dismoney' => $this->argv['dismoney'],
        ];

        if(isset($this->argv['city'])){
            $item['city_id'] = $this->argv['city'];
        }

        (new ExchangeModel())->update_item($item,$this->argv['id']);

        return $this->ret_resp();
    }

    //删除商品
    function del_itemAction(){
        $this->paramlist=[
            'token' => 'string',
            'id' => 'string',                   //商品id
        ];

        $this->check_params();
        $this->check_token();

        (new ExchangeModel())->del_item($this->argv['id']);

        return $this->ret_resp();
    }
    //赠送积分
    function charge_for_scoreAction()
    {
        $this->paramlist = [
            'phone' => 'string',
            'amount' => 'int',
        ];

        $this->check_params();
        $this->check_token();

        $userinfo = (new UserModel())->query_info_by_phone($this->argv['phone']);
        if (empty($userinfo)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "获取用户信息失败!");
        }

        (new ExchangeModel())->record($userinfo['city_id'],$userinfo,SCORE_GATEWAY_PRESENT,$this->argv['amount']);
        (new UserModel())->incr_score($userinfo['id'],$this->argv['amount']);
        //(new ExchangeModel())->present_to($this->city_id, $userinfo, $this->argv['amount'], $this->argv['remark']);

        return $this->ret_resp();
    }
}